package cn.stylefeng.guns.sys.modular.metadataFieldsDetail.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.metadataFieldsDetail.entity.MetadataDetail;
import cn.stylefeng.guns.sys.modular.metadataFieldsDetail.mapper.MetadataDetailMapper;
import cn.stylefeng.guns.sys.modular.metadataFieldsDetail.model.params.MetadataDetailParam;
import cn.stylefeng.guns.sys.modular.metadataFieldsDetail.model.result.MetadataDetailResult;
import cn.stylefeng.guns.sys.modular.metadataFieldsDetail.service.MetadataDetailService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 元数据详情表 服务实现类
 * </p>
 *
 * @author author
 * @since 2020-11-17
 */
@Service
public class MetadataDetailServiceImpl extends ServiceImpl<MetadataDetailMapper, MetadataDetail> implements MetadataDetailService {

    @Override
    public void add(MetadataDetailParam param) {
        MetadataDetail entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(MetadataDetailParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(MetadataDetailParam param) {
        MetadataDetail oldEntity = getOldEntity(param);
        MetadataDetail newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public MetadataDetailResult findBySpec(MetadataDetailParam param) {
        return null;
    }

    @Override
    public List<MetadataDetailResult> findListBySpec(MetadataDetailParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(MetadataDetailParam param) {
        param.setIsDelete(1);
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public int deleteMetadataDetailInfo(Integer id) {
        return this.baseMapper.deleteMetadataDetail(id);
    }

    private Serializable getKey(MetadataDetailParam param) {
        return param.getMetadataDetailId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private MetadataDetail getOldEntity(MetadataDetailParam param) {
        return this.getById(getKey(param));
    }

    private MetadataDetail getEntity(MetadataDetailParam param) {
        MetadataDetail entity = new MetadataDetail();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
